package com.example.autopia.view.Adapters

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.autopia.R
import com.example.autopia.databinding.ViewholderCardBinding
import com.example.autopia.model.Item.Card

class CardAdapter  () :
    RecyclerView.Adapter<CardAdapter.ViewHolder>() {

    private lateinit var list : MutableList<Card>

    fun setData(newList : List<Card>){
        list = newList.toMutableList()
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ViewholderCardBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val news = list[position]
        holder.bind(news)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    inner class ViewHolder(private val binding: ViewholderCardBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(card: Card) {
            binding.tvFio.text = card.fio
            binding.tvDate.text = card.date
            if (card.fio.isEmpty()){
                binding.Card.setBackgroundResource(R.drawable.background_my_balance_card)
                binding.tvFio.visibility= View.INVISIBLE
                binding.tvDate.visibility= View.INVISIBLE
            }
        }
    }
}